package beans;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.SelectEvent;

import daos.AlumnoDAO;
import daosimp.AlumnoDAOIMP;
import modelo.Alumno;
import modelo.Cursoperiodogrupo;
import modelo.Matricula;
import modelo.Persona;

@ManagedBean
@RequestScoped
public class AlumnoBean {

	private Alumno alumno;
	private AlumnoDAO alumnoDAO = new AlumnoDAOIMP();
	private List<Matricula> matriculas;
	private Matricula matriculaSelec;

	public void eventChange(SelectEvent e) {
		try {
			FacesContext.getCurrentInstance().getExternalContext()
					.redirect("alumnoCurso.xhtml");
			FacesContext.getCurrentInstance().getExternalContext()
					.getSessionMap().put("matriculaSelec", matriculaSelec);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	public Alumno getAlumno() {
		return alumno;
	}

	public void setAlumno(Alumno alumno) {
		this.alumno = alumno;
	}

	public boolean verificarSesion() {
		boolean estado = false;
		Persona per = (Persona) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("persona");
		if (per != null) {
			if (per.getRolPersona() == 1) {
				alumno = alumnoDAO.obtenerAlumnoxPersona(per);
				FacesContext.getCurrentInstance().getExternalContext()
						.getSessionMap().put("alumno", alumno);
				llenarDatos();
				FacesContext.getCurrentInstance().getExternalContext()
						.getSessionMap().remove("matriculaSelec");
				estado = true;
			}
		}
		return estado;
	}

	public void llenarDatos() {
		matriculas = new ArrayList<Matricula>();
		for (Object mat : alumno.getMatriculas()) {
			Matricula matx = (Matricula) mat;
			matriculas.add(matx);
		}
	}

	public List<Matricula> getMatriculas() {
		return matriculas;
	}

	public void setMatriculas(List<Matricula> matriculas) {
		this.matriculas = matriculas;
	}

	public Matricula getMatriculaSelec() {
		return matriculaSelec;
	}

	public void setMatriculaSelec(Matricula matriculaSelec) {
		this.matriculaSelec = matriculaSelec;
	}

}
